package org.binglan.youziyuepai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.binglan.youziyuepai.entity.Image;
import org.binglan.youziyuepai.entity.PhotographyTag;
import org.binglan.youziyuepai.entity.School;
import org.binglan.youziyuepai.entity.User;
import org.binglan.youziyuepai.mapper.UserMapper;
import org.binglan.youziyuepai.service.ImageService;
import org.binglan.youziyuepai.service.PhotographyTagService;
import org.binglan.youziyuepai.service.SchoolService;
import org.binglan.youziyuepai.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.binglan.youziyuepai.vo.UserVo;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author sanjin
 * @since 2019-04-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    SchoolService schoolService;

    @Autowired
    PhotographyTagService photographyTagService;

    @Autowired
    ImageService imageService;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User queryUserByOpenid(String openid) {
        User user = new User();
        user.setOpenid(openid);
        return baseMapper.selectOne(new QueryWrapper<>(user));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public User saveUser(User user) {
        user.setId(Sid.nextShort());
        baseMapper.insert(user);
        return user;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User queryUserById(String userId) {
        User user = new User();
        user.setId(userId);
        return baseMapper.selectOne(new QueryWrapper<>(user));
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public UserVo queryUserVoByUserId(String userId) {
        User user = this.queryUserById(userId);
        // 获取与用户有关参数
        School school = schoolService.querySchoolById(user.getSchoolId());
        List<PhotographyTag> tags = photographyTagService.queryTagsByUserId(userId);
        Image avatar = imageService.queryImageById(user.getImageId());

        UserVo userVo = new UserVo();
        userVo.setId(user.getId());
        userVo.setNickname(user.getNickname());
        // 性别，1->男，0->女
        userVo.setGender(user.getGender());
        userVo.setSignature(user.getSignature());
        userVo.setSchool(school);
        userVo.setAvatar(avatar);
        userVo.setBonusPoints(user.getBonusPoints());
        userVo.setTags(tags);
        userVo.setAttentionNum(this.queryAttentionNumByUserId(userId));
        userVo.setFansNum(this.queryFansNumByUserId(userId));

        return userVo;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<UserVo> queryUserVosByUserIds(List<String> userId) {
        if (userId.size() == 0)
            return null;
        return baseMapper.queryUserVosByUserIds(userId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public long queryAttentionNumByUserId(String userId) {

        return baseMapper.queryAttentionNumByUserId(userId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public long queryFansNumByUserId(String userId) {
        return baseMapper.queryFansNumByUserId(userId);
    }

}
